package com.bub.pay.outer.redis.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import javax.annotation.Resource;

@Configuration
public class RedisServerConfig {

	@Autowired
	@Qualifier(value = "smsTopic")
	private ChannelTopic smsTopic;

	@Autowired
	@Qualifier(value = "emailTopic")
	private ChannelTopic emailTopic;

	@Autowired
	@Qualifier(value = "clientTopic")
	private ChannelTopic clientTopic;

	@Resource
	@Qualifier(value = "callBackTopic")
	private ChannelTopic callBackTopic;

	@Autowired
	@Qualifier(value = "jPushTopic")
	private ChannelTopic jPushTopic;

	@Bean(name = "redisCallBackListener")
	public RedisCallBackListener getRedisCallBackListenerChannel() {
		return new RedisCallBackListener();
	}

	@Bean(name = "callBackListenerChannel")
	public MessageListenerAdapter callBackListenerChannel() {
		return new MessageListenerAdapter(getRedisCallBackListenerChannel());
	}

	@Bean(name = "redisSmsListener")
	public RedisSmsListener getRedisSmsListenerChannel() {
		return new RedisSmsListener();
	}

	@Bean(name = "redisEmailListener")
	public RedisEmailListener getRedisEmailListenerChannel() {
		return new RedisEmailListener();
	}

	@Bean(name = "redisHttpListener")
	public RedisHttpListener getRedisHttpListenerChannel() {
		return new RedisHttpListener();
	}

	@Bean(name = "redisJPushListener")
	public RedisJPushListener getRedisJPushChannel(){
		return new RedisJPushListener();
	}

	@Bean(name = "smsListenerChannel")
	public MessageListenerAdapter smsListenerChannel() {
		return new MessageListenerAdapter(getRedisSmsListenerChannel());
	}

	@Bean(name = "emailListenerChannel")
	public MessageListenerAdapter emailListenerChannel() {
		return new MessageListenerAdapter(getRedisEmailListenerChannel());
	}

	@Bean(name = "httpListenerChannel")
	public MessageListenerAdapter httpListenerChannel() {
		return new MessageListenerAdapter(getRedisHttpListenerChannel());
	}

	@Bean(name = "jPushListenerChannel")
	public MessageListenerAdapter jPushListenerChannel(){
		return new MessageListenerAdapter(getRedisJPushChannel());
	}

	@Bean
	public RedisMessageListenerContainer redisContainer(LettuceConnectionFactory factory) {
		final RedisMessageListenerContainer container = new RedisMessageListenerContainer();
		container.setConnectionFactory(factory);
		container.addMessageListener(smsListenerChannel(), smsTopic);
		container.addMessageListener(emailListenerChannel(), emailTopic);
		container.addMessageListener(httpListenerChannel(), clientTopic);
		container.addMessageListener(callBackListenerChannel(), callBackTopic);
		container.addMessageListener(jPushListenerChannel(),jPushTopic);
		return container;
	}

}
